package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.WmThreadLocalUtils;
import com.heima.model.user.dtos.UserRelationDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRelationService;
import com.netflix.loadbalancer.RetryRule;
import jdk.nashorn.internal.runtime.regexp.joni.constants.OPCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author lo
 * @date 2021/9/29
 */
@Service
@Slf4j
public class ApUserRelationServiceImpl implements ApUserRelationService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    ApUserMapper apUserMapper;

    /**
     * 关注或者取关
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDTO dto) {
        //1.校验参数
        //文章作者必须存在
        if (dto.getAuthorApUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "作者的用户id不存在");
        }

        //2.获取当前登录用户的id
        Integer loginId = WmThreadLocalUtils.getUser().getId();
        if (loginId == 0) {
            //说明用户没用登录,不能关注
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3.自己不能关注自己
        if (dto.getAuthorApUserId().equals(loginId)) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "不可以自己关注自己哦~");
        }
        Short operation = dto.getOperation();
        if (operation != 0 && operation != 1) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }

        try {
            //  开启  redis  的事务
            redisTemplate.multi();
            //5.关注或者取关
            if (operation == 0){
                //4.查看之前有没有关注过
                Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST+loginId,dto.getAuthorApUserId() + "");
                if (score != null) {
                    //说明关注过
                    CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "不能重复关注哦");
                }
                //关注行为
                //给登录用户列表添加一个作者id
                redisTemplate.opsForZSet().add(UserRelationConstants.FOLLOW_LIST + loginId,String.valueOf(dto.getAuthorApUserId()),System.currentTimeMillis());
                //给文章作者添加一个登录用户的id
                redisTemplate.opsForZSet().add(UserRelationConstants.FANS_LIST+dto.getAuthorApUserId(), String.valueOf(loginId),System.currentTimeMillis());

            }else {
                // 取关行为
                //给登录用户列表删除一个作者id
                redisTemplate.opsForZSet().remove(UserRelationConstants.FOLLOW_LIST + loginId,String.valueOf(dto.getAuthorApUserId()));
                //给文章作者删除一个登录用户的id
                redisTemplate.opsForZSet().remove(UserRelationConstants.FANS_LIST+dto.getAuthorApUserId(), String.valueOf(loginId));
            }
            //  提交事务
            redisTemplate.exec();
        } catch (Exception e) {
            e.printStackTrace();
            // 如果有异常   取消事务
            redisTemplate.discard(); // 取消事务
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,"关注过程中出现异常");
        }

        return ResponseResult.okResult();
    }
}
